﻿using System;
using System.Windows.Forms;


namespace TextGenerator
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// Prepares a generation.
        /// </summary>
        private void BeginGeneration()
        {
            // Set up the progressbar.
            this.toolStripProgressBar1.Minimum = 0;
            this.toolStripProgressBar1.Maximum = 100;
            this.toolStripProgressBar1.Value = 0;
            this.toolStripProgressBar1.Visible = true;

            // Clear the results box.
            this.rtbResults.Text = String.Empty;

            // Disable GUI elements.
            this.rtbPattern.Enabled = false;
            this.rtbResults.Enabled = false;
            this.generateToolStripMenuItem.Enabled = false;
            this.cancelOngoingGenerationToolStripMenuItem.Enabled = true;
            this.clearresultsToolStripMenuItem.Enabled = false;
            this.copyResultsToClipboardToolStripMenuItem.Enabled = false;

            // Start the timer and the worker process.
            this.timer.Start();
        }

        /// <summary>
        /// Cleans up after a generation has been cancelled.
        /// </summary>
        private void CancelGeneration()
        {
            // Stop the timer.
            this.timer.Stop();

            // Update GUI elements.
            this.tsslGeneratedIn.Text = String.Format("Generation cancelled.");
            this.tsslStatistics.Visible = false;

            // Enable GUI elements.
            this.rtbPattern.Enabled = true;
            this.rtbResults.Enabled = true;
            this.generateToolStripMenuItem.Enabled = true;
            this.cancelOngoingGenerationToolStripMenuItem.Enabled = false;
            this.clearresultsToolStripMenuItem.Enabled = false;
            this.copyResultsToClipboardToolStripMenuItem.Enabled = false;

            // Hide the progressbar.
            this.toolStripProgressBar1.Visible = false;

            // Dispose of the background worker.
            this.backgroundWorker1.Dispose();

            // Return focus to the pattern box.
            this.rtbPattern.Focus();
        }

        /// <summary>
        /// Finishes up after a generation.
        /// </summary>
        private void EndGeneration()
        {
            // Stop the timer.
            this.timer.Stop();

            // Display the generation time.
            this.tsslGeneratedIn.Text = String.Format("Generated in {0:F4} seconds.", this.timer.Duration);

            // Display statistics.
            int lines = (this.permutator.AppendNewLine) ? this.rtbResults.Lines.Length - 1 : this.rtbResults.Lines.Length;
            this.tsslStatistics.Text = String.Format("Lines: {0:N0}, Characters: {1:N0}", lines, this.rtbResults.TextLength);
            this.tsslStatistics.Visible = true;

            // Enable GUI elements.
            this.rtbPattern.Enabled = true;
            this.rtbResults.Enabled = true;
            this.generateToolStripMenuItem.Enabled = true;
            this.cancelOngoingGenerationToolStripMenuItem.Enabled = false;
            this.clearresultsToolStripMenuItem.Enabled = true;
            this.copyResultsToClipboardToolStripMenuItem.Enabled = true;

            // Hide the progressbar.
            this.toolStripProgressBar1.Visible = false;

            // Dispose of the background worker.
            this.backgroundWorker1.Dispose();

            // Return focus to the pattern box.
            this.rtbPattern.Focus();
        }
    }
}